﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="HttpHelper.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：HttpHelper.cs
// 创建时间：2014-09-13 10:16
// 修改时间：2014-09-13 10:16
// </summary>
// ---------------------------------------------------------------------------------------
#endregion
namespace WebCommon
{
    using System;
    using System.IO;
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    internal class AcceptAllCertificatePolicy : ICertificatePolicy
    {
        public AcceptAllCertificatePolicy()
        {
        }

        public bool CheckValidationResult(ServicePoint sPoint,
           X509Certificate cert, WebRequest wRequest, int certProb)
        {
            // Always accept  
            return true;
        }
    }
    /// <summary>
    /// http
    /// </summary>
    public class HttpHelper
    {
        /// <summary>
        /// true为模式post，false为get
        /// </summary>
        public bool Method { get; set; }

        /// <summary>
        /// URL地址
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// 发送的数据
        /// </summary>
        public string Data { get; set; }
        /// <summary>
        /// 设置ContentType，默认application/x-www-form-urlencoded
        /// </summary>
        public string ContentType { get; set; }

        public HttpHelper()
        {
            this.Method = true;
            this.Data = "";
        }
        public string PostPage()
        {
            HttpWebRequest request = null;
            Stream writer = null;
            try
            {
                ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();

                byte[] data = Encoding.UTF8.GetBytes(this.Data);
                request = (HttpWebRequest)WebRequest.Create(this.Url);
                request.Timeout = 30000;
                request.AllowAutoRedirect = false;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.LongLength;
                if (Method)
                {
                    writer = request.GetRequestStream();
                    writer.Write(data, 0, data.Length);
                    writer.Close();
                }



                var response = (HttpWebResponse)request.GetResponse();

                var res = new StreamReader(response.GetResponseStream());
                var restring = res.ReadToEnd();
                return restring;

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                if (request != null) request = null;
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
            }

        }
        internal class AcceptAllCertificatePolicy : ICertificatePolicy
        {
            public AcceptAllCertificatePolicy()
            {
            }

            public bool CheckValidationResult(ServicePoint sPoint,
               X509Certificate cert, WebRequest wRequest, int certProb)
            {
                // Always accept  
                return true;
            }
        }

        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="url"></param>
        /// <param name="path"></param>
        public static void GetImages(string url,string path)
        {

            //创建一个request 同时可以配置requst其余属性

            System.Net.WebRequest imgRequst = System.Net.WebRequest.Create(url);

            //在这里我是以流的方式保存图片
    
            var responseStream = imgRequst.GetResponse().GetResponseStream();
            if (responseStream != null)
            {
                System.Drawing.Image downImage = System.Drawing.Image.FromStream(responseStream); 

                downImage.Save(path);

                downImage.Dispose();
            } 
            //用完一定要释放 
        }
        public static string GetPage(string url,  string contentType= "application/x-www-form-urlencoded")
        {
            HttpWebRequest request = null;
            Stream writer = null;
            try
            {
              
   
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 30000;
                request.AllowAutoRedirect = false;
                request.Method = "Get";
                request.ContentType = contentType; 
     
           
                var response = (HttpWebResponse)request.GetResponse();

                var res = new StreamReader(stream: response.GetResponseStream());
                var restring = res.ReadToEnd();
                return restring;

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                if (request != null) request = null;
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
            }
            return string.Empty;
        }

        public static string PostPage(string url, string postData = "")
        {
            HttpWebRequest request = null;
            Stream writer = null;
            try
            {
                ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
                byte[] data = Encoding.UTF8.GetBytes(postData);
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 30000;
                request.AllowAutoRedirect = false;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.LongLength;


                writer = request.GetRequestStream();
                writer.Write(data, 0, data.Length);
                writer.Flush();
                var response = (HttpWebResponse)request.GetResponse();

                var res = new StreamReader(response.GetResponseStream());
                var restring = res.ReadToEnd();
                return restring;

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                if (request != null) request = null;
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
            }
            return string.Empty;
        }public static string PostJson(string url, string postData = "")
        {
            HttpWebRequest request = null;
            Stream writer = null;
            try
            {
                ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
                byte[] data = Encoding.UTF8.GetBytes(postData);
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 30000;
                request.AllowAutoRedirect = false;
                request.Method = "POST";
                request.ContentType = "application/json";
                request.ContentLength = data.LongLength;


                writer = request.GetRequestStream();
                writer.Write(data, 0, data.Length);
                writer.Flush();
                var response = (HttpWebResponse)request.GetResponse();

                var res = new StreamReader(response.GetResponseStream());
                var restring = res.ReadToEnd();
                return restring;

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                if (request != null) request = null;
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
            }
            return string.Empty;
        }
    }




}
